package com.efounder.builder.base.data;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Comparator;

import com.efounder.builder.base.util.*;
//import com.efounder.db.*;
import com.efounder.eai.data.*;
import com.efounder.pub.util.*;

public class DataSetUtils {
  /**
   *
   */
  protected DataSetUtils() {
  }
  /**
   *
   * @param stmt Statement
   * @param tableName String
   * @param fields String
   * @param where String
   * @return ResultSet
   * @throws Exception
   */
  public static ResultSet getResultSet(JParamObject paramObject,Statement stmt,
                                       String tableName,String fields,String where) throws Exception {
    ResultSet resultSet = null;
//    tableName = DBTools.getDBAObject(paramObject,tableName);
//    String SQL = DBTools.selectSql(tableName,fields,where);
   // resultSet = stmt.executeQuery(SQL);
    return resultSet;
  }
  /**
   *
   * @param resultSet ResultSet
   * @param dataSet EFDataSet
   * @return EFDataSet
   * @throws Exception
   */
  public static EFDataSet resultSet2DataSet(ResultSet resultSet,EFDataSet dataSet) throws Exception {
//    ESPRowSet rowSet = null;
//    while ( resultSet.next() ) {
//      // 生成ESPRowSet
//      rowSet = EFRowSet.getInstance();
//      // 将ResultSet中的一行生成到RowSet
//      rowSet = resultSet2RowSet(resultSet,rowSet);
//      // 插入DataSet中
//      dataSet.insertRowSet(rowSet);
//      // 形成主键索引
//      dataSet.buildPrimeKeyIndex(rowSet);
//    }
//    return dataSet;
    return resultSet2DataSet(resultSet, dataSet, true);
  }

  /**
   *
   * @param resultSet ResultSet
   * @param dataSet EFDataSet
   * @param btrim boolean
   * @return EFDataSet
   * @throws Exception
   */
  public static EFDataSet resultSet2DataSet(ResultSet resultSet,EFDataSet dataSet, boolean btrim) throws Exception {
    ESPRowSet rowSet = null;
    while ( resultSet.next() ) {
      // 生成ESPRowSet
      rowSet = EFRowSet.getInstance();
      // 将ResultSet中的一行生成到RowSet
      rowSet = resultSet2RowSet(resultSet,rowSet,btrim);
      // 插入DataSet中
      dataSet.insertRowSet(rowSet);
      // 形成主键索引
      dataSet.buildPrimeKeyIndex(rowSet);
      
    }

    return dataSet;
  }
  
/**
   *
   * @param resultSet ResultSet
   * @param dataSet EFDataSet
   * @param btrim boolean
   * @param unique boolean
   * @return EFDataSet
   * @throws Exception
   */
  public static EFDataSet resultSet2DataSet(ResultSet resultSet,EFDataSet dataSet, boolean btrim, boolean unique) throws Exception {
    ESPRowSet rowSet = null;
    while ( resultSet.next() ) {
      // 生成ESPRowSet
      rowSet = EFRowSet.getInstance();
      // 将ResultSet中的一行生成到RowSet
      rowSet = resultSet2RowSet(resultSet,rowSet,btrim);
      // 插入DataSet中
      dataSet.insertRowSet(rowSet, unique);
      // 形成主键索引
      dataSet.buildPrimeKeyIndex(rowSet);
    }
    return dataSet;
  }

  /**
   *
   * @param resultSet ResultSet
   * @param rowSet ESPRowSet
   * @param btrim boolean
   * @return ESPRowSet
   * @throws Exception
   */
  public static ESPRowSet resultSet2RowSet(ResultSet resultSet,ESPRowSet rowSet, boolean btrim) throws Exception {
  /*  // 不动原来的方法
    if (btrim)
      return resultSet2RowSet(resultSet, rowSet);
    // 取出ResultSet的MetaData
    ResultSetMetaData rmd = resultSet.getMetaData();
    // 处理每一列数据
    int colCount = rmd.getColumnCount();String colName=null;Object value;
    for(int i=1;i<=colCount;i++) {
      colName = rmd.getColumnName(i);
      value = resultSet.getObject(colName);
      //BLOB CLOB不传递
      if (rmd.getColumnType(i) == java.sql.Types.BLOB
          || rmd.getColumnType(i) == java.sql.Types.CLOB) {
        value = null;
      }
      // 如果
      if ( rmd.getColumnType(i) == java.sql.Types.DATE ||
           rmd.getColumnType(i) == java.sql.Types.TIME ||
           rmd.getColumnType(i) == java.sql.Types.TIMESTAMP ) {
        value = resultSet.getObject(colName);
        if ( value instanceof oracle.sql.Datum ){
          value = ((oracle.sql.Datum)value).timestampValue();
        }
      }else{//没法弄了，
         value = resultSet.getObject(colName);
        if ( value instanceof oracle.sql.Datum ){
          continue;
        }
      }

      if (value != null && value instanceof String)
        rowSet.putString(colName, (String)value, btrim);
      else if (value != null)
        rowSet.putObject(colName, value);
    }*/
    /**
     *
     */
    rowSet.setDataStatus(ESPRowSet._DATA_STATUS_NORMAL_);
    return rowSet;
  }

  /**
   *
   * @param resultSet ResultSet
   * @param efDataSet EFDataSet
   * @return EFDataSet
   * @throws Exception
   */
  public static ESPRowSet resultSet2RowSet(ResultSet resultSet,ESPRowSet rowSet) throws Exception {
   /* // 取出ResultSet的MetaData
    ResultSetMetaData rmd = resultSet.getMetaData();
    // 处理每一列数据
    int colCount = rmd.getColumnCount();String colName=null;Object value;
    for(int i=1;i<=colCount;i++) {
      colName = rmd.getColumnName(i);
      value = resultSet.getObject(colName);
      // 调整一下顺序，很重要
//      //BLOB CLOB不传递
//      if (rmd.getColumnType(i) == java.sql.Types.BLOB
//          || rmd.getColumnType(i) == java.sql.Types.CLOB) {
//          value = null;
//      }
      // 如果
      if ( rmd.getColumnType(i) == java.sql.Types.DATE ||
           rmd.getColumnType(i) == java.sql.Types.TIME ||
           rmd.getColumnType(i) == java.sql.Types.TIMESTAMP ) {
        value = resultSet.getObject(colName);
        if ( value instanceof oracle.sql.Datum ){
          value = ((oracle.sql.Datum)value).timestampValue();
        }
      }else{//没法弄了，
         value = resultSet.getObject(colName);
        if ( value instanceof oracle.sql.Datum ){
          continue;
        }
      }
      //BLOB CLOB不传递
      if (rmd.getColumnType(i) == java.sql.Types.BLOB
          || rmd.getColumnType(i) == java.sql.Types.CLOB) {
        value = null;
      }
      if(value!=null)
        rowSet.putObject(colName,value);
    }*/
    /**
     *
     */
    rowSet.setDataStatus(ESPRowSet._DATA_STATUS_NORMAL_);
    return rowSet;
  }

  /**
   * 对一个EFDataSet排序，可以指定一列或者多列以及每列进行升序或者降序
   *
   * @param ds EFDataSet
   * @param column String[]
   * @param type String[]
   * @param ascending boolean[]
   */
  public static void sortEFDataSet(EFDataSet ds, String[] column, boolean[] ascending,Object[] compar) {
	/*  ComparatorChain chain = new ComparatorChain();
	    for (int i = 0; i < column.length; i++) {
	      boolean ascend = true;
	      if (ascending == null) ascend = true;
	      else ascend = ascending[i];
	      Comparator cp=null;
	      if(compar!=null&&compar.length>i)
	    	  cp=(Comparator)compar[i];
	      EFRowSetComparator comparator = new EFRowSetComparator(column[i], ascend,cp);
	      chain.addComparator(comparator);
	    }
	    //一个比较器也没有，直接返回
	    if (chain.size() == 0 || ds.getRowCount() == 0) return;
	    Object[] rss = ds.getRowSetList().toArray();
	    java.util.Arrays.sort(rss, chain);
	    //把排序后的RowSet赋回去
	    ds.getRowSetList().clear();
	    for (int i = 0; i < rss.length; i++) {
	        EFRowSet rs = (EFRowSet)rss[i];
	        ds.addRowSet(rs);
	    }*/
  }
  public static void sortEFDataSet(EFDataSet ds, String[] column, boolean[] ascending) {
	  sortEFDataSet( ds, column, ascending,null);
  }

  /**
   * 恢复每个RowSet在某列排序前的索引
   *
   * @param ds EFDataSet
   * @param column String
   */
  public static void recoverRowSetIndex(EFDataSet ds, String column) {
    String[] columns = new String[]{column + "_ROWID"};
    sortEFDataSet(ds, columns, null);
  }

  /**
   * 设置每个RowSet在某列排序前的索引
   *
   * @param ds EFDataSet
   * @param column String
   */
  public static void setRowSetIndex(EFDataSet ds, String column) {
    java.util.List rowSets = ds.getRowSetList();
    if (rowSets == null) return;
    for (int i = 0; i < ds.getRowCount(); i++) {
      EFRowSet  rs = ds.getRowSet(i);
      String rowid = StringFunction.FillZeroFromBegin(i, 10); //10位长度，很给力了
      rs.putString(column+"_ROWID", rowid);
    }
  }

  /**
   * 并集
   * @param lists List
   * @return List
   */
  public static java.util.List unionList(java.util.List<java.util.List> lists) {
    java.util.Set hashSet = new java.util.HashSet();
    java.util.List list = null;
    for (int i = 0; lists != null && i < lists.size(); i++) {
      list = lists.get(i);
      if (list == null) continue;
      hashSet.addAll(list);
    }
    java.util.List result = new java.util.ArrayList();
    result.addAll(hashSet);
    return result;
  }

  /**
   * 交集
   * @param lists List
   * @return List
   */
  public static java.util.List intersectList(java.util.List<java.util.List> lists) {
    java.util.List unionList = unionList(lists);
    java.util.List result = new java.util.ArrayList();
    java.util.List list = null;
    Object   value = null;
    for (int i = 0; unionList != null && i < unionList.size(); i++) {
      value = unionList.get(i);
      boolean exists = true;
      for (int j = 0; lists != null && j < lists.size(); j++) {
        list = lists.get(j);
        if (!list.contains(value)) {
          exists = false;
          break;
        }
      }
      if (exists) result.add(value);
    }
    return result;
  }


}
